package com.bigcat.gulimall.demo.openai;// 导入必要的库

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 用ai写的贪吃蛇小游戏
 */
public class SnakeGame extends JFrame {

    // 定义游戏区域的大小
    private final int WIDTH = 300;
    private final int HEIGHT = 300;

    // 定义贪吃蛇的初始位置和大小
    private final int SNAKE_SIZE = 10;
    private final int SNAKE_X = WIDTH / 2;
    private final int SNAKE_Y = HEIGHT / 2;

    // 定义食物的初始位置和大小
    private final int FOOD_SIZE = 10;
    private int foodX;
    private int foodY;

    // 定义贪吃蛇的移动速度
    private final int DELAY = 100;

    // 定义贪吃蛇的移动方向
    private enum Direction { UP, DOWN, LEFT, RIGHT };
    private Direction direction = Direction.RIGHT;

    // 定义贪吃蛇的身体
    private int[] snakeX = new int[WIDTH * HEIGHT / SNAKE_SIZE];
    private int[] snakeY = new int[WIDTH * HEIGHT / SNAKE_SIZE];
    private int snakeLength = 1;

    // 定义游戏是否结束
    private boolean gameOver = false;

    public SnakeGame() {
        // 设置窗口的标题和大小
        setTitle("贪吃蛇小游戏");
        setSize(WIDTH, HEIGHT);

        // 随机生成食物的位置
        generateFood();

        // 设置窗口关闭时的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 添加键盘监听器
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP:
                        if (direction != Direction.DOWN) {
                            direction = Direction.UP;
                        }
                        break;
                    case KeyEvent.VK_DOWN:
                        if (direction != Direction.UP) {
                            direction = Direction.DOWN;
                        }
                        break;
                    case KeyEvent.VK_LEFT:
                        if (direction != Direction.RIGHT) {
                            direction = Direction.LEFT;
                        }
                        break;
                    case KeyEvent.VK_RIGHT:
                        if (direction != Direction.LEFT) {
                            direction = Direction.RIGHT;
                        }
                        break;
                }
            }
        });

        // 启动游戏循环
        Timer timer = new Timer(DELAY, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!gameOver) {
                    move();
                    checkCollision();
                    repaint();
                }
            }
        });
        timer.start();
    }

    // 生成食物的位置
    private void generateFood() {
        int maxX = (WIDTH - FOOD_SIZE) / SNAKE_SIZE;
        int maxY = (HEIGHT - FOOD_SIZE) / SNAKE_SIZE;
        foodX = (int) (Math.random() * maxX) * SNAKE_SIZE;
        foodY = (int) (Math.random() * maxY) * SNAKE_SIZE;
    }

    // 移动贪吃蛇
    private void move() {
        // 移动身体
        for (int i = snakeLength - 1; i > 0; i--) {
            snakeX[i] = snakeX[i - 1];
            snakeY[i] = snakeY[i - 1];
        }

        // 移动头部
        switch (direction) {
            case UP:
                snakeY[0] -= SNAKE_SIZE;
                break;
            case DOWN:
                snakeY[0] += SNAKE_SIZE;
                break;
            case LEFT:
                snakeX[0] -= SNAKE_SIZE;
                break;
            case RIGHT:
                snakeX[0] += SNAKE_SIZE;
                break;

        }
    }

    // 检查碰撞
    private void checkCollision() {
        // 检查是否吃到食物
        if (snakeX[0] == foodX && snakeY[0] == foodY) {
            snakeLength++;
            generateFood();
        }

        // 检查是否撞到墙壁
        if (snakeX[0] < 0 || snakeX[0] >= WIDTH || snakeY[0] < 0 || snakeY[0] >= HEIGHT) {
            gameOver = true;
        }

        // 检查是否撞到自己的身体
        for (int i = 1; i < snakeLength; i++) {
            if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
                gameOver = true;
            }
        }
    }

    // 绘制游戏界面
    @Override
    public void paint(Graphics g) {
        super.paint(g);

        // 绘制食物
        g.setColor(Color.RED);
        g.fillRect(foodX, foodY, FOOD_SIZE, FOOD_SIZE);

        // 绘制贪吃蛇
        g.setColor(Color.GREEN);
        for (int i = 0; i < snakeLength; i++) {
            g.fillRect(snakeX[i], snakeY[i], SNAKE_SIZE, SNAKE_SIZE);
        }

        // 绘制游戏结束文字
        if (gameOver) {
            g.setColor(Color.BLACK);
            g.setFont(new Font("宋体", Font.BOLD, 40));
            g.drawString("游戏结束", WIDTH / 2 - 100, HEIGHT / 2);
        }
    }

    public static void main(String[] args) {
        new SnakeGame().setVisible(true);
    }
}
